package com.bolt.report.business.modules.link.controller;

import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Results;
import com.bolt.report.business.modules.field.dto.ReportDbFieldQueryCriteria;
import com.bolt.report.business.modules.link.dto.ReportLinkDTO;
import com.bolt.report.business.modules.link.dto.converter.ReportLinkConverter;
import com.bolt.report.business.modules.link.entity.ReportLinkEntity;
import com.bolt.report.business.modules.link.service.ReportLinkService;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * Auto Generate Javadoc
 *
 * @author 日期:2023-03-30 16:17:07
 *         超链接配置表
 **/
@RestController
@RequestMapping("/report/design/link")
@RequiredArgsConstructor
@Api(tags = "超链接配置表")
public class ReportLinkController {

    private final ReportLinkService reportLinkService;

    /**
     * 列表分页
     *
     * @return
     */
    @PostMapping(value = "/page")
    @ApiOperation(notes = "列表分页列表分页", httpMethod = "GET", value = "列表分页")
    public ResultMessage list(Pageable pageable, ReportDbFieldQueryCriteria queryCriteria) {
        PageInfo<ReportLinkEntity> pageInfo = reportLinkService.findPage(pageable, (root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, queryCriteria, criteriaBuilder));
        return Results.success(pageInfo.map(K -> ReportLinkConverter.toDTO(K)));
    }

    /**
     * 根据ID操作
     */
    @PostMapping("/get")
    @ApiOperation(notes = "根据ID查询", httpMethod = "POST", value = "根据ID查询")
    public ResultMessage<List<ReportLinkDTO>> get(@RequestBody List<String> ids) {
        List<ReportLinkEntity> entities = reportLinkService.findAll(k->k.lambda().in(ReportLinkEntity::getId,ids).build());
        return Results.success(ReportLinkConverter.toDTO(entities));
    }

    /**
     * 编辑操作
     *
     * @param dao 表单数据
     * @return
     */
    @PostMapping
    @ApiOperation(notes = "新增", httpMethod = "POST", value = "新增")
    public ResultMessage<ReportLinkEntity> create(@Validated @RequestBody ReportLinkDTO dao) {
        ReportLinkEntity entity = reportLinkService.saveOrUpdate(dao);
        return Results.success(entity);
    }


    /**
     * 删除操作
     */
    @DeleteMapping
    @ApiOperation(notes = "根据ID删除", httpMethod = "DELETE", value = "根据ID删除")
    public ResultMessage<Collection<String>> doDelete(@RequestBody Set<String> ids) {
        for (String id : ids) {
            reportLinkService.deleteById(id);
        }
        return Results.success(ids);
    }

}